package com.yanqu.road.dao.impl.monopolymarket.resgrab;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.monopolymarket.MonopolyMarketBeautyInfo;
import com.yanqu.road.entity.monopolymarket.MonopolyMarketPatronsInfo;
import com.yanqu.road.entity.monopolymarket.resgrab.MonopolyMarketResGrabPointData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description
 * @Author cwq
 * @Data 2021/9/6 21:21
 */
public class MonopolyMarketResGrabPointDataDaoImpl extends TempDataObjectDao<MonopolyMarketResGrabPointData> {
    public static String getMonopolyMarketResGrabPointDataSql = "select * from `t_c_monopolymarket_resgrab_point` where `groupId`=? and `type`=?";
    public Map<Integer, MonopolyMarketResGrabPointData> getMonopolyMarketResGrabPointDataMap(long groupId,int type) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, MonopolyMarketResGrabPointData> map = new HashMap();
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(getMonopolyMarketResGrabPointDataSql);
            int i = 1;
            call.setLong(i++, groupId);
            call.setInt(i++, type);
            rs = call.executeQuery();
            while (rs.next()) {
                MonopolyMarketResGrabPointData pointData = parse(rs);
                map.put(pointData.getPosIndex(), pointData);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String updateMonopolyMarketResGrabPointDataSql = "update `t_c_monopolymarket_resgrab_point` set userId=?,remainTime=?,receiveLastTime=?,produceRewardLastTime=?,seatTime=?,endTime=?,buffValues=?,objectInfoList=?,changeTime=? where groupId=? and type=? and posIndex = ?";
    @Override
    protected boolean updateData(MonopolyMarketResGrabPointData monopolyMarketResGrabPointData) {
        monopolyMarketResGrabPointData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, monopolyMarketResGrabPointData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketResGrabPointData.getRemainTime()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketResGrabPointData.getReceiveLastTime()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketResGrabPointData.getProduceRewardLastTime()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketResGrabPointData.getSeatTime()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketResGrabPointData.getEndTime()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketResGrabPointData.getBuffValues()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(monopolyMarketResGrabPointData.getObjectInfoList())));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketResGrabPointData.getChangeTime()));
        para.put(i++, new DbParameter(Types.BIGINT, monopolyMarketResGrabPointData.getGroupId()));
        para.put(i++, new DbParameter(Types.TINYINT, monopolyMarketResGrabPointData.getType()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketResGrabPointData.getPosIndex()));
        if (getDb().executeNoneQuery(updateMonopolyMarketResGrabPointDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    /**
     * 批量更新据点信息
     * @param list
     * @return
     */
    public boolean batchUpdate(List<MonopolyMarketResGrabPointData> list){
        getDb().setConnName(DbNameStrategy.getCrossDb());
        List<Map<Integer, DbParameter>> mapList = new ArrayList<>();
        for (MonopolyMarketResGrabPointData monopolyMarketResGrabPointData : list) {
            Map<Integer, DbParameter> para = new HashMap<>();
            int i = 1;
            monopolyMarketResGrabPointData.setBeginWriteTime();
            para.put(i++, new DbParameter(Types.BIGINT, monopolyMarketResGrabPointData.getUserId()));
            para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketResGrabPointData.getRemainTime()));
            para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketResGrabPointData.getReceiveLastTime()));
            para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketResGrabPointData.getProduceRewardLastTime()));
            para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketResGrabPointData.getSeatTime()));
            para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketResGrabPointData.getEndTime()));
            para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketResGrabPointData.getBuffValues()));
            para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(monopolyMarketResGrabPointData.getObjectInfoList())));
            para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketResGrabPointData.getChangeTime()));
            para.put(i++, new DbParameter(Types.BIGINT, monopolyMarketResGrabPointData.getGroupId()));
            para.put(i++, new DbParameter(Types.TINYINT, monopolyMarketResGrabPointData.getType()));
            para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketResGrabPointData.getPosIndex()));
            mapList.add(para);
            monopolyMarketResGrabPointData.setNoneOption();
        }
        return getDb().executeBatchQuery(updateMonopolyMarketResGrabPointDataSql, mapList) > -1;
    }


    public static String insertMonopolyMarketResGrabPointDataSql = "insert into `t_c_monopolymarket_resgrab_point`(groupId,type,posIndex,userId,remainTime,receiveLastTime,produceRewardLastTime,seatTime,endTime,buffValues,objectInfoList,changeTime) values (?,?,?,?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(MonopolyMarketResGrabPointData monopolyMarketResGrabPointData) {
        monopolyMarketResGrabPointData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, monopolyMarketResGrabPointData.getGroupId()));
        para.put(i++, new DbParameter(Types.TINYINT, monopolyMarketResGrabPointData.getType()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketResGrabPointData.getPosIndex()));
        para.put(i++, new DbParameter(Types.BIGINT, monopolyMarketResGrabPointData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketResGrabPointData.getRemainTime()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketResGrabPointData.getReceiveLastTime()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketResGrabPointData.getProduceRewardLastTime()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketResGrabPointData.getSeatTime()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketResGrabPointData.getEndTime()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketResGrabPointData.getBuffValues()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(monopolyMarketResGrabPointData.getObjectInfoList())));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketResGrabPointData.getChangeTime()));
        if (getDb().executeNoneQuery(insertMonopolyMarketResGrabPointDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    /**
     * 批量更新据点信息
     * @param list
     * @return
     */
    public boolean batchAdd(List<MonopolyMarketResGrabPointData> list){
        getDb().setConnName(DbNameStrategy.getCrossDb());
        List<Map<Integer, DbParameter>> mapList = new ArrayList<>();
        for (MonopolyMarketResGrabPointData monopolyMarketResGrabPointData : list) {
            Map<Integer, DbParameter> para = new HashMap<>();
            int i = 1;
            monopolyMarketResGrabPointData.setBeginWriteTime();
            para.put(i++, new DbParameter(Types.BIGINT, monopolyMarketResGrabPointData.getGroupId()));
            para.put(i++, new DbParameter(Types.TINYINT, monopolyMarketResGrabPointData.getType()));
            para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketResGrabPointData.getPosIndex()));
            para.put(i++, new DbParameter(Types.BIGINT, monopolyMarketResGrabPointData.getUserId()));
            para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketResGrabPointData.getRemainTime()));
            para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketResGrabPointData.getReceiveLastTime()));
            para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketResGrabPointData.getProduceRewardLastTime()));
            para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketResGrabPointData.getSeatTime()));
            para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketResGrabPointData.getEndTime()));
            para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketResGrabPointData.getBuffValues()));
            para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(monopolyMarketResGrabPointData.getObjectInfoList())));
            para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketResGrabPointData.getChangeTime()));
            mapList.add(para);
            monopolyMarketResGrabPointData.setNoneOption();
        }

        return getDb().executeBatchQuery(insertMonopolyMarketResGrabPointDataSql, mapList) > -1;
    }



    @Override
    protected MonopolyMarketResGrabPointData parseData(ResultSet rs) throws SQLException {
        MonopolyMarketResGrabPointData monopolyMarketResGrabPointData = new MonopolyMarketResGrabPointData();
        monopolyMarketResGrabPointData.setGroupId(rs.getLong("groupId"));
        monopolyMarketResGrabPointData.setType(rs.getByte("type"));
        monopolyMarketResGrabPointData.setPosIndex(rs.getInt("posIndex"));
        monopolyMarketResGrabPointData.setUserId(rs.getLong("userId"));
        monopolyMarketResGrabPointData.setRemainTime(rs.getInt("remainTime"));
        monopolyMarketResGrabPointData.setReceiveLastTime(rs.getInt("receiveLastTime"));
        monopolyMarketResGrabPointData.setProduceRewardLastTime(rs.getInt("produceRewardLastTime"));
        monopolyMarketResGrabPointData.setSeatTime(rs.getInt("seatTime"));
        monopolyMarketResGrabPointData.setEndTime(rs.getInt("endTime"));
        monopolyMarketResGrabPointData.setBuffValues(rs.getInt("buffValues"));
        if(monopolyMarketResGrabPointData.getType() == 2){
            List<MonopolyMarketPatronsInfo> list = (JSON.parseObject(rs.getString("objectInfoList"), new TypeReference<java.util.List<com.yanqu.road.entity.monopolymarket.MonopolyMarketPatronsInfo>>() {}));
            monopolyMarketResGrabPointData.getObjectInfoList().addAll(list);
        }else if(monopolyMarketResGrabPointData.getType() == 3){
            List<MonopolyMarketBeautyInfo> list = (JSON.parseObject(rs.getString("objectInfoList"), new TypeReference<java.util.List<com.yanqu.road.entity.monopolymarket.MonopolyMarketBeautyInfo>>() {}));
            monopolyMarketResGrabPointData.getObjectInfoList().addAll(list);
        }
        monopolyMarketResGrabPointData.setChangeTime(rs.getInt("changeTime"));

        return monopolyMarketResGrabPointData;
    }
}
